package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacGroupRoleMapper;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.group.GroupRoleActionListDto;
import com.csun.cmny.provider.model.dto.group.GroupRoleDto;
import com.csun.cmny.provider.model.dto.group.GroupRoleMenuListDto;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-07-21 13:07
 * @Description: the class uac group role service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UacGroupRoleServiceImpl extends BaseService<UacGroupRole>
        implements UacGroupRoleService {

    @Resource
    private UacGroupRoleMapper uacGroupRoleMapper;
    @Resource
    private UacGroupRoleMenuService uacGroupRoleMenuService;
    @Resource
    private UacGroupRoleActionService uacGroupRoleActionService;
    @Resource
    private UacGroupService uacGroupService;
    @Resource
    private UacRoleService uacRoleService;
    @Resource
    private UacRoleActionService uacRoleActionService;
    @Resource
    private UacRoleMenuService uacRoleMenuService;

    /**
     * 批量添加机构角色
     *
     * @param groupId    the group id
     * @param roleIdList the role id list
     *
     * @return the int.
     */
    @Override
    public Integer batchSaveGroupRole(Long groupId, List<Long> roleIdList) {

        return uacGroupRoleMapper.batchSaveGroupRole(groupId, roleIdList);
    }

    /**
     * 根据机构Id查询角色列表
     *
     * @param groupId the group id
     *
     * @return the list.
     */
    @Override
    public List<UacRole> queryRoleListByGroupId(Long groupId) {

        return uacGroupRoleMapper.queryRoleListByGroupId(groupId);
    }

    /**
     * 绑定机构角色
     *
     * @param groupRoleDto the group role dto
     * @return the int.
     */
    @Override
    public Integer assignGroupRole(GroupRoleDto groupRoleDto) {

        Integer result = 0;

        // 查询角色是否存在
        UacRole uacRole = uacRoleService.selectByKey(groupRoleDto.getRoleId());
        if (PublicUtil.isEmpty(uacRole)) {
            throw new UacBizException(ErrorCodeEnum.UAC10012009);
        }

        // 查询机构是否存在
        UacGroup uacGroup = uacGroupService.selectByKey(groupRoleDto.getGroupId());
        if (PublicUtil.isEmpty(uacGroup)) {
            throw new UacBizException(ErrorCodeEnum.UAC10015016);
        }

        // 查询该机构是否已经拥有该角色
        UacGroupRole uacGroupRole = new UacGroupRole();
        BeanUtils.copyProperties(groupRoleDto, uacGroupRole);
        int count = uacGroupRoleMapper.selectCount(uacGroupRole);
        if (count > 0) {
            throw new UacBizException(ErrorCodeEnum.UAC10015021);
        }
        // 添加机构-角色
        result += uacGroupRoleMapper.insertSelective(uacGroupRole);

                // 查询全部的角色-功能
        List<UacRoleAction> uacRoleActionList = uacRoleActionService.listByRoleId(groupRoleDto.getRoleId());

        // 批量添加全部的机构-角色-菜单
        if (PublicUtil.isNotEmpty(uacRoleActionList)) {
            result += uacGroupRoleActionService
                    .batchSaveGroupRoleAction(groupRoleDto.getGroupId(), uacRoleActionList);
        }

        // 查询全部的角色-菜单
        List<UacRoleMenu> uacRoleMenuList = uacRoleMenuService.listByRoleId(groupRoleDto.getRoleId());

        // 批量添加全部的机构-角色-菜单
        if (PublicUtil.isNotEmpty(uacRoleMenuList)) {
            result += uacGroupRoleMenuService
                    .batchSaveGroupRoleMenu(groupRoleDto.getGroupId(), uacRoleMenuList);
        }

        return result;
    }

    /**
     * 移除机构角色
     *
     * @param groupRoleDto the group role dto
     * @return the int.
     */
    @Override
    public Integer deleteGroupRole(GroupRoleDto groupRoleDto) {

        // 移除机构-角色-菜单
        GroupRoleMenuListDto groupRoleMenuListDto = new GroupRoleMenuListDto();
        BeanUtils.copyProperties(groupRoleDto, groupRoleMenuListDto);
        Integer result = uacGroupRoleMenuService.batchDeleteRoleMenu(groupRoleMenuListDto);

        // 移除机构-角色-功能
        GroupRoleActionListDto groupRoleActionListDto = new GroupRoleActionListDto();
        BeanUtils.copyProperties(groupRoleDto, groupRoleActionListDto);
        result += uacGroupRoleActionService.batchDeleteRoleAction(groupRoleActionListDto);

        // 移除机构-角色
        UacGroupRole uacGroupRole = new UacGroupRole();
        BeanUtils.copyProperties(groupRoleDto, uacGroupRole);
        result += uacGroupRoleMapper.delete(uacGroupRole);

        return result;
    }

    /**
     * query not bind role list by group id
     *
     * @param groupId the group id
     *
     * @return the list.
     */
    @Override
    public List<UacRole> queryNotBindRoleListByGroupId(Long groupId) {

        return uacGroupRoleMapper.queryNotBindRoleListByGroupId(groupId);
    }
}
